
package net.atremmedia.gwt.actionjava.examples.frame;

import net.atremmedia.gwt.actionjava.examples.AJExample;
import actionjava.display.Graphics;
import actionjava.display.Shape;
import actionjava.display.TextField;
import actionjava.events.mouse.MouseEvent;
import actionjava.events.mouse.MouseEvent.MouseEventType;
import actionjava.events.mouse.MouseOutHandler;
import actionjava.events.mouse.MouseOverHandler;
import actionjava.events.mouse.StageDownHandler;
import com.google.gwt.dom.client.Style.Cursor;
import com.google.gwt.user.client.ui.RootPanel;

public class AJExampleButton
{
	private double x;
	private double y;
	private int index;
	private Shape buttonBG;
	private TextField buttonTitle;

	private AJExample example;
	private AJExampleOverview overview;
	private StageDownHandler downHandler = new StageDownHandler()
	{
		@Override
		protected void stageDownHandler(MouseEvent event)
		{
			overview.loadExample(example);
		}
	};

	public AJExampleButton(double x, double y, int index, AJExample example, AJExampleOverview overview)
	{
		this.x = x;
		this.y = y;
		this.index = index;
		this.example = example;
		this.overview = overview;
		this.initialize();
	}

	private void initialize()
	{
		createBG();
		overview.addChild(buttonBG);

		createTitleField();
		overview.addChild(buttonTitle);

		setEvents(overview);
	}

	private void setEvents(final AJExampleOverview overview)
	{
		buttonBG.addEventListener(MouseEventType.MOUSE_OVER, new MouseOverHandler()
		{
			@Override
			protected void mouseOverHandler(MouseEvent event)
			{
				RootPanel.get().getElement().getStyle().setCursor(Cursor.POINTER);
				buttonTitle.setColor("#FF0000");
				registerClickListener(true);
			}
		});

		buttonBG.addEventListener(MouseEventType.MOUSE_OUT, new MouseOutHandler()
		{
			@Override
			protected void mouseOutHandler(MouseEvent event)
			{
				RootPanel.get().getElement().getStyle().setCursor(Cursor.DEFAULT);
				buttonTitle.setColor("#000000");
				registerClickListener(false);
			}
		});
	}

	private void registerClickListener(boolean register)
	{
		if(register) {
			overview.addEventListener(MouseEventType.STAGE_MOUSE_DOWN, downHandler);
		} else {
			RootPanel.get().getElement().getStyle().setCursor(Cursor.DEFAULT);
			overview.removeEventListener(MouseEventType.STAGE_MOUSE_DOWN, downHandler);
		}
	}

	private void createBG()
	{
		buttonBG = new Shape();
		buttonBG.setX(x);
		buttonBG.setY(y);

		Graphics graphs = buttonBG.getGraphics();
		graphs.beginFill("#FFFFFF");
		graphs.drawRect(0, 0, 200, 15);
		graphs.endFill();
	}

	private void createTitleField()
	{
		buttonTitle = new TextField(index + ") " + example.getName(), "Arial", "#000000");
		buttonTitle.setSize(13);
		buttonTitle.setX(x);
		buttonTitle.setY(y);
	}

	public int getIndex()
	{
		return index;
	}

	public void destroy()
	{
		registerClickListener(false);
		overview.removeChild(buttonTitle);
		overview.removeChild(buttonBG);
	}
}
